/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tudou.searchcontrol.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tudou.searchcontrol.common.SourceType;
import com.tudou.searchcontrol.model.Album;
import com.tudou.searchcontrol.model.Playsource;
import com.tudou.searchcontrol.model.Video;
import com.tudou.searchcontrol.model.PageInfo;
import com.tudou.searchcontrol.service.IAlbumService;
import com.tudou.searchcontrol.service.IPlaysourceService;
import com.tudou.searchcontrol.service.IVideoService;
import com.tudou.searchcontrol.utils.ReadPage;



@Controller
@RequestMapping("/video/*")
public class VideoAction extends BaseAction{

	@Autowired
	private IVideoService videoService;
	
	@Autowired
	private IPlaysourceService playsourceService;
	
//	@Autowired
//	private IAlbumService albumService;
	
	private static final ObjectMapper objectMapper = new ObjectMapper();
	
	@RequestMapping("list")
	public String list(HttpServletRequest request, HttpServletResponse response, ModelMap model,Video video,PageInfo pageInfo){
		Map searchMap = new HashMap();
		pageInfo =videoService.getVideoByPage(searchMap,pageInfo);
		
		model.put("PageInfo", pageInfo);
		model.put("searchMap", searchMap);

		return "/video/videoList";
	}
	
	@RequestMapping("view")
	public String view(HttpServletRequest request, HttpServletResponse response, ModelMap model,Long id){
		Video video = videoService.getVideoById(id);
		model.put("machineSetting", video);
		return "/video/videoView";
	}
	
	@RequestMapping("getVideoInfo")
	public String getVideoInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model,Video video,Playsource playsource){
		model.put("video", video);

		String playUrl = getTudouVideoContent(request,response,playsource.getPlayurl());
		try {
			Map<String, Object> map = objectMapper.readValue(playUrl, Map.class);
			Map<String, Object> map1 = (Map<String, Object>)map.get("repasteInfo");
			Map<String, Object> map2 = (Map<String, Object>)map1.get("itemInfo");
			Map<String, Object> drameInfo = (Map<String, Object>)map1.get("drameInfo");
			
			playsource.setSwfUrl((String)map2.get("outerPlayerUrl"));
			playsource.setPirurl((String)map2.get("picUrl"));
			playsource.setIntroduction((String)map2.get("description"));
			playsource.setShowTime((String)map2.get("pubDate"));
			playsource.setType((String)map2.get("mediaType"));
			playsource.setTitle((String)map2.get("title"));
			playsource.setLength((Integer)map2.get("totalTime")/1000+"");
			
			if(drameInfo != null){
				
			}
			playsource.setSourcetype(SourceType.TUDOU);
			
			
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.put("playsource", playsource);
		return "/video/videoInfo";
	}
	
	
	
	
	@RequestMapping("edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, ModelMap model,Video video){
		if(video.getId() != null){
			video = videoService.getVideoById(video.getId());
		}
		model.put("video", video);
		return "/video/videoEdit";
	}
	
	@RequestMapping("save")
	public String save(HttpServletRequest request, HttpServletResponse response, ModelMap model,Video video,Playsource playsource,PageInfo pageInfo){
		int i = playsourceService.saveOrUpdatePlaysource(playsource);
		if(i>0){	
			video.setPlayId(playsource.getId());
			i = videoService.saveOrUpdateVideo(video);
		}
		
		if(i>0){
			playsource.setVdeoId(video.getId());
			playsourceService.saveOrUpdatePlaysource(playsource);
		}
		
		
		Video newVideo = new Video();
		
		return this.list(request, response, model, newVideo,pageInfo);
	}
	
	
	@RequestMapping("delete")
	public String delete(HttpServletRequest request, HttpServletResponse response, ModelMap model,Video video,String delIds,PageInfo pageInfo){
		if(StringUtils.isEmpty(delIds)){
			delIds = video.getId().toString();
		}
		int i = videoService.deleteVideo(delIds);
		
		Video newVideo = new Video();
		return this.list(request, response, model, newVideo,pageInfo);
	}
	
	
	private String getTudouVideoContent(HttpServletRequest request, HttpServletResponse response,String vidioUrl){
		ReadPage readPage = new ReadPage(request,response);
		String url = "http://api.tudou.com/v3/gw?method=repaste.info.get&appKey=b8005c8e0c2d1085&url="+vidioUrl;
		String pageInfo = readPage.include(url,"utf-8");
		return pageInfo;
	}
}